<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="导航名称" prop="naName">
        <el-input
          v-model="queryParams.naName"
          placeholder="请输入导航名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:aNavigation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:aNavigation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:aNavigation:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aNavigationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="导航名称" align="center" prop="naName" />
      <el-table-column label="导航封面图" align="center" prop="naImage" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.naImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="导航类型" align="center" prop="naType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.na_type" :value="scope.row.naType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否上线" align="center" prop="isShow">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isShow" @change="handleSwitchChange(scope.row)"  active-text="上线" inactive-text="下线" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="numSoft" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:aNavigation:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:aNavigation:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改首页导航对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="导航名称" prop="naName">
          <el-input v-model="form.naName" placeholder="请输入导航名称" :maxlength="5" />
        </el-form-item>
        <el-form-item label="导航封面图" prop="naImage" >
          <image-upload v-model="form.naImage" :limit="1" fileRatio="1:1" :fileType="['png','jpg','jpeg']"/>
        </el-form-item>
        <el-form-item label="导航类型" prop="naType">
          <el-select v-model="form.naType" placeholder="请选择导航类型" @change="naTypeChange">
            <el-option
              v-for="dict in dict.type.na_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="站内跳转" prop="insideUrl" v-if="form.naType === '1'">-->
<!--          <el-select v-model="form.insideUrl" placeholder="请选择站内跳转">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.inside_type"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-form-item :label="lableUrl" prop="naUrl" v-if="form.naType ">
          <el-input v-model="form.naUrl" :placeholder="'请输入'+ lableUrl" />
<!--          <el-input v-model="form.naUrl" :placeholder="'请输入'+ lableUrl"  v-if="form.naType != '1'"/>-->
<!--          <select-check-infor v-model="form.naUrl" v-if="form.insideUrl === '0' && form.naType === '1'"/>-->
<!--          <select-data-info v-model="form.naUrl" v-if="form.insideUrl === '1' && form.naType === '1'"/>-->
<!--          <select-train-infor v-model="form.naUrl" v-if="form.insideUrl === '2' && form.naType === '1'"/>-->
          <span v-if="form.naType === '4' || form.naType === '1'" class="demonstration">
            <div v-for="item in aTipsList"><span style="color: red;">{{item.tipsName}}</span>:  {{item.tipsUrl}}</div>
          </span>
        </el-form-item>
        <el-form-item label="是否上线" prop="isShow">
          <el-switch v-model="form.isShow" active-text="上线" inactive-text="下线" />
        </el-form-item>
        <el-form-item label="排序" prop="numSoft">
          <el-input-number v-model="form.numSoft" :min="0" :max="9999" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listANavigation, getANavigation, delANavigation, addANavigation, updateANavigation } from "@/api/system/aNavigation";
import { listATips, getATips, delATips, addATips, updateATips } from "@/api/system/aTips";
import selectDataInfo from "@/views/system/aDataInfo/SelectDataInfo";
import SelectCheckInfor from "@/views/system/aDataInfo/SelectCheckInfor";
import SelectTrainInfor from "@/views/system/aDataInfo/SelectTrainInfor";
export default {
  name: "ANavigation",
  dicts: ['sys_yes_no', 'na_type','inside_type'],
  components:{
    SelectTrainInfor,
    SelectCheckInfor,
    selectDataInfo
  },
  // watch:{
  //   "form.naType":{
  //     handler(val){
  //       if(val !== undefined){
  //         this.lableUrl = this.lableList[val]
          // if(val === "1" && !this.form.insideUrl){
          //   this.$set(this.form,"insideUrl",'0')
          // }else if (val !== '1'){
          //   this.$set(this.form,"insideUrl",null)
          //   this.form.naUrl = this.form.naUrl;
          // }
  //       }
  //     },
  //   }
  // },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 首页导航表格数据
      aNavigationList: [],
      lableList :['外链地址','地址url','公众号地址',"APPID","自定义"],
      lableListUrl: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        naName: null,
        naImage: null,
        naType: null,
        naUrl: null,
        isShow: null,
        numSoft: null
      },
      // 表单参数
      form: {
        insideUrl: null,
        naUrl:null,
      },
      // 表单校验
      rules: {
        naName: [
          { required: true, message: "导航名称不能为空", trigger: "blur" }
        ],
        naImage: [
          { required: true, message: "导航封面图不能为空", trigger: "blur" }
        ],
        naType: [
          { required: true, message: "导航类型不能为空", trigger: "change" }
        ],
        naUrl: [
          { required: true, message: "导航跳转路径不能为空", trigger: "blur" }
        ],
        insideUrl: [
          { required: true, message: "站内跳转不能为空", trigger: "change" }
        ],
        // isShow: [
        //   { required: true, message: "是否上线不能为空", trigger: "change" }
        // ],
        numSoft: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      },
      lableUrl:"外链地址",
      aTipsList:[],
    };
  },
  created() {
    this.getList();
    this.getTipsList();
  },

  methods: {
    getTipsList() {
      let queryParams = {
        pageNum: 1,
        pageSize: 1000,
      }
      listATips(queryParams).then(response => {
        this.aTipsList = response.rows;
      });
    },
    naTypeChange(val){
      if(val == 1){
        this.lableListUrl = true;
      }else{
        this.lableListUrl = false;
      }
      this.form.naUrl = null;
      this.lableUrl = this.lableList[val];
    },
    handleSwitchChange(row){
      updateANavigation(row).then(res=>{
        if(res.code == 200){
          this.$modal.msgSuccess("修改成功");
          this.getList();
        }else{
          this.$modal.msgError(res.msg);
        }
      })
    },
    /** 查询首页导航列表 */
    getList() {
      this.loading = true;
      listANavigation(this.queryParams).then(response => {
        this.aNavigationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        naName: null,
        naImage: null,
        naType: null,
        naUrl: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        isShow: null,
        numSoft: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加首页导航";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getANavigation(id).then(response => {
        this.form = response.data;
        console.log(this.form)
        if(this.form.naType == 1){
          this.lableListUrl = true;
        }
        this.open = true;
        this.title = "修改首页导航";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateANavigation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getTipsList();
            });
          } else {
            addANavigation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getTipsList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除首页导航编号为"' + ids + '"的数据项？').then(function() {
        return delANavigation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/aNavigation/export', {
        ...this.queryParams
      }, `aNavigation_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
